package com.hsw.habit.controller;

import com.hsw.habit.frame.annotation.CurrentUser;
import com.hsw.habit.frame.annotation.NoLoginRequired;
import com.hsw.habit.frame.utils.MD5Util;
import com.hsw.habit.frame.utils.OssUtil;
import com.hsw.habit.frame.utils.TextUtils;
import com.hsw.habit.model.AvatarBean;
import com.hsw.habit.model.JsonData;
import com.hsw.habit.model.TaskHelper;
import com.hsw.habit.model.User;
import com.hsw.habit.service.UserService;
import com.qcloud.cos.model.Bucket;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;


    @NoLoginRequired
    @RequestMapping("/login")
    public JsonData login(String phone,String password) {
        return userService.login(phone, password);
    }

    @NoLoginRequired
    @RequestMapping("/register")
    public JsonData register(User user) {
        return userService.register(user);
    }

    @RequestMapping("/getUserInfo")
    public JsonData getUserInfo(@CurrentUser User user) {
        user.setPhone(TaskHelper.formatPhone(user.getPhone()));
        user.setPassword("");
        if (TextUtils.isEmpty(user.getAvatar())) {
            user.setAvatar("https://hsw-1251827437.cos.ap-shanghai.myqcloud.com/192-192.png");
        }
        return JsonData.success(user);
    }

    @NoLoginRequired
    @RequestMapping("/getBkList")
    public JsonData getBkList() {
        List<Bucket> list = OssUtil.getInstance().getBucketList();
        return JsonData.success(list);
    }

    @RequestMapping("/updatePwd")
    public JsonData changePwd(@CurrentUser User user, String oldPwd, String newPwd) {
        if (TextUtils.isEmpty(oldPwd) || TextUtils.isEmpty(newPwd)) {
            return JsonData.error(4001,"请输入密码");
        }

        Logger.getLogger("--").warning(oldPwd + "--" + MD5Util.md5(oldPwd) + "--" + user.getPassword());

        if (!user.getPassword().equals(MD5Util.md5(oldPwd))) {
            return JsonData.error(4001,"密码不正确");
        }

        user.setPassword(MD5Util.md5(newPwd));
        userService.update(user);
        return JsonData.success();
    }

    @RequestMapping("/updateUserName")
    public JsonData updateUser(@CurrentUser User user,String userName) {
        user.setUserName(userName);
        boolean result = userService.update(user);
        if (result) {
            return JsonData.success();
        } else {
            return JsonData.error();
        }
    }

    @RequestMapping("/updatePhone")
    public JsonData updateUser(@CurrentUser User user,String newPhone, String oldPhone) {
        if (TextUtils.isEmpty(oldPhone) || TextUtils.isEmpty(newPhone)) {
            return JsonData.error(4001,"请输入");
        }

        if (oldPhone.equals(newPhone)) {
            return JsonData.error(4001,"两次输入的手机一致");
        }

        if (null == userService.findUserByPhone(oldPhone)) {
            return JsonData.error(4001,"旧手机号不正确");
        }

        if (null != userService.findUserByPhone(newPhone)) {
            return JsonData.error(4001,"手机号已注册过");
        }
        user.setPhone(newPhone);
        boolean result = userService.update(user);
        if (result) {
            return JsonData.success();
        } else {
            return JsonData.error();
        }
    }



    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public JsonData uploadAvatar(@CurrentUser User user, @RequestParam("image") MultipartFile file) throws IOException {

        String url = OssUtil.getInstance().uploadFileByInputStream(file.getInputStream());

        if (!TextUtils.isEmpty(url)) {
            user.setAvatar(url);
            userService.update(user);
        }

        return JsonData.success(new AvatarBean(url));
    }

}
